prepare($students_query);
$students_stmt->execute([$acyear, $term, $klass]);
$students = $students_stmt->fetchAll(PDO::FETCH_ASSOC);
// Get subjects
$subjects_query = "SELECT DISTINCT subject FROM marks
WHERE acyear = ?
AND term = ?
AND klass = ?
ORDER BY subject";
$subjects_stmt = $DBcon->prepare($subjects_query);
$subjects_stmt->execute([$acyear, $term, $klass]);
$subjects = $subjects_stmt->fetchAll(PDO::FETCH_ASSOC);
if(count($students) > 0 && count($subjects) > 0) {
$total_subjects = count($subjects);
// Calculate totals and averages for all students first
$student_data = [];
foreach($students as $student) {
$regno = $student['regno'];
$total_score = 0;
// Calculate total score for this student across ALL subjects
foreach($subjects as $subject) {
$mark_query = $DBcon->prepare("SELECT test, exam FROM marks WHERE regno = ? AND subject = ? AND acyear = ? AND term = ? AND klass = ?");
$mark_query->execute([$regno, $subject['subject'], $acyear, $term, $klass]);
$mark = $mark_query->fetch(PDO::FETCH_ASSOC);
if($mark) {
$total_score += $mark['test'] + $mark['exam'];
}
// If student doesn't have marks for a subject, it counts as 0 (not taken)
}
// Calculate average based on total number of subjects offered by class
$average = $total_subjects > 0 ? $total_score / $total_subjects : 0;
$student_data[] = [
'regno' => $regno,
'fullname' => $student['fullname'],
'total_score' => $total_score,
'average' => $average
];
}
// Sort students by average (descending) for position
usort($student_data, function($a, $b) {
return $b['average'] <=> $a['average'];
});
// Assign positions (handle ties)
$student_positions = [];
$current_position = 1;
$previous_average = null;
$skip_count = 0;
foreach($student_data as $index => $student) {
if($previous_average !== null && $student['average'] < $previous_average) {
$current_position = $index + 1;
}
$student_positions[$student['regno']] = $current_position;
$previous_average = $student['average'];
}
?>
Class Result Sheet
Raymond Schools Nkpor
Class Result Sheet
Academic Year
Term
Class
Total Students
Total Subjects
Average calculated based on subjects offered by the class
STUDENT NAME (Registration No.)
TOTAL SCORE
AVERAGE Out of subjects
POSITION
Test
Exam
Total
prepare($students_query);
$students_display->execute([$acyear, $term, $klass]);
$students_for_display = $students_display->fetchAll(PDO::FETCH_ASSOC);
foreach($students_for_display as $student):
$regno = $student['regno'];
$position = $student_positions[$regno] ?? '-';
$position_class = '';
if($position == 1) $position_class = 'position-1';
elseif($position == 2) $position_class = 'position-2';
elseif($position == 3) $position_class = 'position-3';
// Calculate this student's total for display
$student_total = 0;
foreach($subjects as $subject) {
$mark_query = $DBcon->prepare("SELECT test, exam FROM marks WHERE regno = ? AND subject = ? AND acyear = ? AND term = ? AND klass = ?");
$mark_query->execute([$regno, $subject['subject'], $acyear, $term, $klass]);
$mark = $mark_query->fetch(PDO::FETCH_ASSOC);
if($mark) {
$student_total += $mark['test'] + $mark['exam'];
}
}
// Get the pre-calculated average (based on total subjects)
$student_average = 0;
foreach($student_data as $data) {
if($data['regno'] == $regno) {
$student_average = $data['average'];
break;
}
}
?>
Reg No:
prepare("SELECT test, exam FROM marks WHERE regno = ? AND subject = ? AND acyear = ? AND term = ? AND klass = ?");
$mark_query->execute([$regno, $subject['subject'], $acyear, $term, $klass]);
$mark = $mark_query->fetch(PDO::FETCH_ASSOC);
if($mark):
$test = $mark['test'];
$exam = $mark['exam'];
$subject_total = $test + $exam;
?>
-
-
-
Class Size: Students
Subjects Offered: Subjects
Academic Year:
Term:
Average Calculation: Total Score ÷ Subjects = Student Average